<?php

class Cache{
	
	private $is_expired = true;
	private $cache_file;
	
	public $cache_contents;
	
	function __construct(){
		
	}
	
	public function verify($obj){
		$this->cache_file = implode('_', $obj->system->mvc_action).'_cache';
		
		if(
			file_exists(CACHE_DIR.$this->cache_file.'.dat') && 
			self::cache_is_not_expired(CACHE_DIR.$this->cache_file.'.exp')
			){
			$this->cache_contents = self::cache_get_contents(CACHE_DIR.$this->cache_file.'.dat');
		}
		
	}
	
	private static function cache_get_contents($filename){
		$content = file_get_contents($filename);
		return unserialize(gzuncompress(base64_decode($content)));
	}
	
	private static function cache_is_not_expired($filename){
		$exp = file_get_contents($filename);
		if($exp >= time())return true;
		
		else return false;
	}
	
	public function cachize($contents){
		$cont = base64_encode(gzcompress(serialize($contents),9));
		file_put_contents(CACHE_DIR.$this->cache_file.'.dat', $cont);
		file_put_contents(CACHE_DIR.$this->cache_file.'.exp', time() + VIOLA_CACHE_DURATION);
	}
	
}
